<?php

/**
 * 帮麦查验商城BBC JDK
 * @author chenshubo
 */
namespace App\Service\VendorClient\BmBBCGoodsCheckServiceApi;
use Ly\Unit\Http;
use think\facade\Config;
use Ly\MsService\Service\QueryService\QueryGoodsMicService;
use Ly\Unit\AmqpLib\rabbitMqMbBbc;
use App\Service\BaseService;
use App\Service\Code\Normal;

class BmBBCGoodsCheckServiceApiClient extends BaseService
{
    
    /**
     * 查询商场推送创建订单接口
     * @param array $params
     * @return mixed
     */
    public function OrderApply( $params = [] )
    {
        if( empty($params['orderInfo']) || empty($params['orderItem']) ) {
            return Normal::returnCode(Normal::FAIL,"except param orderInfo or orderItem");
        }
        
        $orderInfo      = $params['orderInfo'];
        $orderItem      = $params['orderItem'];
        $orderBBCdata   = [];
        $productAmt     = 0;
        
        //处理商品组
        foreach ($orderItem as $item ) {
        
            $price          = $item['price'];
            $goodsTax       = bcmul($price, $item['tax_rate'],2);
            $actual_price   = bcsub($price, $goodsTax,2);
            $quantity       = $item['quantity'];
            
            $orderBBCdata [] = [
                'sku_num'       => $item['skucode'],
                'goods_num'     => $quantity,
                'price'         => $actual_price,
            ];
            $productAmt = bcadd($productAmt, bcmul($quantity, $actual_price,2),2);
        }
        
        //支付时间
        $pay_time       = date('Y-m-d H:i:s',$orderInfo['pay_time']);
        //支付流水号
        $pay_sn         = $orderInfo['pay_sn'];
        
        //下单数据
        $orderData = [
            'order_sn'          => $orderInfo['supplier_dsn'],
            'pay_sn'            => $orderInfo['pay_sn'],
            'buyer_name'        => $orderInfo['buyer_realname'],
            'add_time'          => $orderInfo['cd'],
            'payment_time'      => $orderInfo['pay_time'],
            'goods_amount'      => $productAmt,
            'order_amount'      => $orderInfo['order_money'],
            'province'          => $orderInfo['province'],
            'city'              => $orderInfo['city'],
            'area'              => $orderInfo['district'],
            'reciver_address'   => $orderInfo['receive_address'],
            'reciver_name'      => $orderInfo['receive_name'],
            'reciver_mobile'    => $orderInfo['recveive_phone'],
            'reciver_idcard'    => $orderInfo['buyer_idcard'],
            'buyer_idcard'      => $orderInfo['buyer_idcard'],
            'buyer_mobile'      => $orderInfo['buyer_phone'],
            'shipping_fee'      => $orderInfo['deliver_money'],
            'tax_fee'           => $orderInfo['tax'],
            'shipping_code'     => '',
            'notifyTime'        => $orderInfo['pay_time'],
            'merchNo'           => $orderInfo['buyer_phone'],
            'merchParam'        => '',
            'tradeDate'         => $orderInfo['cd'],
            'accNo'             => $orderInfo['pay_sn'],
            'accDate'           => $orderInfo['pay_time'],
            'goods'             => $orderBBCdata
        ];
        
        $requestParam = [
            'method'    => 'addOrder',
            'data'      => [
                'param' => json_encode($orderData,JSON_UNESCAPED_UNICODE)
            ],
        ];
        return $this->execute($requestParam);
    }
    
    /**
     * 查验商城订单发货通知接口
     * @param array $param
     */
    public function OrderShippingNotify( $param = [] ) 
    {
        if( empty($param['supplier_dsn']) || empty($param['shipping_code']) || empty($param['express_code'])) {
            return Normal::returnCode(Normal::FAIL,"except param supplier_csn or shipping_code or express_code");
        }
        
        $orderData = [
            'order_sn'      => $param['supplier_dsn'],
            'shipping_code' => $param['shipping_code'],
            'express_code'  => $param['express_code']
        ];
        
        $requestParam = [
            'method'    => 'updateOrder',
            'data'      => $orderData,
        ];
        
        return $this->execute($requestParam);
    }
    
    /**
     * CURL请求
     * @param $params
     * @return mixed
     */
    public function execute( $param = [] )  
    {
        $applyUrl = Config::get("app.m_api_module.mbbbc.acount.{$param['method']}");
        if( empty($applyUrl) ) {
            return Normal::returnCode(Normal::FAIL,"查验商城接口信息不能为空");
        }
        
        $data       = $param['data'];
        
        //通知消息队列
        rabbitMqMbBbc::instance()->publish([
            'MessageType'   => $param['method'],
            'url'           => $applyUrl,
            'message'       => json_encode($data,JSON_UNESCAPED_UNICODE)
        ]);
        
        return Normal::returnCode(Normal::SUC,'发布成功'); 
        
        
        $start_time = microtime(true);
        $res        = Http::apiRequest($applyUrl,$data,'post');
        $response   = json_decode($res,true);
        if( !isset($response['status']) ) {
            return Normal::returnCode(Normal::FAIL,"请求查验BBC商城接口失败");
        }
        
        $httpCode   = Http::getHttpCode();
        QueryGoodsMicService::instance()->addRequestLog([
            'interface_code'    => 'OrderBbcApply',
            'request_url'       => $applyUrl,
            'request_param'     => $data,
            'response_code'     => $httpCode,
            'response_data'     => $res,
            'start_time'        => $start_time,
        ]);
        
        if( $response['status'] == 1 ) {
            return Normal::returnCode(Normal::SUC,$response['msg']);
        }
        
        return Normal::returnCode(Normal::FAIL,"请求查验BBC商城接口失败【{$response['msg']}】");
    }
}
